package com.gxuwz.zlc.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gxuwz.zlc.common.Result;
import com.gxuwz.zlc.entity.JobContent;
import com.gxuwz.zlc.entity.Student;
import com.gxuwz.zlc.mapper.JobContentMapper;
import com.gxuwz.zlc.service.JobContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.apache.commons.lang3.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 工作内容
 */
@Controller
@RequestMapping("/jobContent")
public class JobContentController {

    @Autowired
    private JobContentService jobContentService;

    @Autowired
    private JobContentMapper jobContentMapper;

    // 查询所有
    @RequestMapping("/findAll")
    @ResponseBody
    public List<JobContent> findJobContent(@RequestParam(name = "str", required = false) String str){
        // 构造查询条件
        QueryWrapper<JobContent> queryWrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(str)){
            queryWrapper.like("job_time",str);
            List<JobContent> jobContentList = jobContentMapper.selectList(queryWrapper);
            return jobContentList;
        }
        List<JobContent> jobContentList = jobContentService.findJobContent();
        return jobContentList;
    }

    /**
     * 查询一个
     * @param jobContentId
     * @return
     */
    @GetMapping("/findOne/{jobContentId}")
    @ResponseBody
    public Result get(@PathVariable String jobContentId){
        JobContent jobJobContentById = jobContentService.findJobJobContentById(jobContentId);
        // code=0,msg=成功
        return Result.success(jobJobContentById);
    }

    // 修改
    @RequestMapping("/update")
    @ResponseBody
    public Map updateJobContent(@RequestBody JobContent jobContent){
        jobContentService.updateJobContent(jobContent);
        Map<String,String> map = new HashMap<>();
        map.put("code","200");
        map.put("msg","修改成功");
        return map;
    }

    /**
     * 实验室助理
     * 添加工作内容
     * @param jobContent
     * @return
     */
    @RequestMapping("/add")
    @ResponseBody
    public Map addJobContent(@RequestBody JobContent jobContent){
        Map<String,String> map = new HashMap<>();
        try {
            jobContent.setSystemTime(new Date());
            jobContentService.addJobContent(jobContent);
            map.put("code","200");
            map.put("msg","添加成功");
        } catch (Exception e) {
            map.put("code","500");
            map.put("msg","添加失败");
            e.printStackTrace();
        }
        return map;
    }


    /**
     * 根据助理id查询工作内容
     * @param assistantId
     * @return
     */
    @GetMapping("/selectAll/{assistantId}")
    @ResponseBody
    public List<JobContent> getByAssistantId(@PathVariable String assistantId){
        List<JobContent> jobJobContentByAssistantId = jobContentService.findJobJobContentByAssistantId(assistantId);
        // code=0,msg=成功
        return jobJobContentByAssistantId;
    }

}
